﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using Microsoft.WindowsLive.Id.Client;
//using System.Windows.Forms;

//namespace Bitter.Win
//{
//    public class WindowsLive
//    {
//        public IdentityManager IdentityManager { get; set; }
//        public Identity Identity { get; set; }

//        private WindowsLive()
//        {
//            Initialize();
//        }

//        private static WindowsLive _instance;

//        private string defaultUserName;
//        private string currentUserName;

//        public static WindowsLive Instance
//        {
//            get
//            {
//                if (_instance == null)
//                {
//                    _instance = new WindowsLive();
//                }

//                return _instance;
//            }
//        }

//        public void Initialize()
//        {
//            try
//            {
//                IdentityManager = IdentityManager.CreateInstance("SondreB.com;bitter@sondreb.com;Bitter", "Bitter");
//            }
//            catch (WLLogOnException wlex)
//            {
//                //Check to see if FlowUrl is defined.
//                if (wlex.FlowUrl != null)
//                {
//                    //If FlowUrl is defined, direct user to the web page to correct the error.
//                    MessageBox.Show(wlex.ErrorString + "Please go to " + wlex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error");
//                }
//                else
//                {
//                    //If FlowUrl is not defined, simply display the ErrorString.
//                    MessageBox.Show(wlex.ErrorString);
//                }
//            }

//            //Check the config file to see if a default user is defined.
//            defaultUserName = Properties.Settings.Default.DefaultUserName;
//            if (!String.IsNullOrEmpty(defaultUserName))
//            {
//                TrySilentSignIn();
//            }
//            else
//            {
//                //If no default user is defined, try instantiating the global Identity object instance from scratch.
//                try
//                {
//                    Identity = IdentityManager.CreateIdentity();
//                }
//                catch (WLLogOnException wlex)
//                {
//                    //Check to see if FlowUrl is defined.
//                    if (wlex.FlowUrl != null)
//                    {
//                        //If FlowUrl is defined, direct user to the web page to correct the error.
//                        MessageBox.Show(wlex.ErrorString + "Please go to " + wlex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error");
//                    }
//                    else
//                    {
//                        //If FlowUrl is not defined, simply display the ErrorString.
//                        MessageBox.Show(wlex.ErrorString);
//                    }
//                }
//            }

//            UpdateDisplay();
//        }

//        private void UpdateDisplay()
//        {
//            //Sign Out
//        }

//        private void TrySilentSignIn()
//        {
//            //Try instantiating the global Identity object instance with the username from the config file.
//            try
//            {
//                Identity = IdentityManager.CreateIdentity(defaultUserName);
//            }
//            catch (WLLogOnException wlex)
//            {
//                //Check to see if FlowUrl is defined.
//                if (wlex.FlowUrl != null)
//                {
//                    //If FlowUrl is defined, direct user to the web page to correct the error.
//                    MessageBox.Show(wlex.ErrorString + "Please go to " + wlex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error");
//                }
//                else
//                {
//                    //If FlowUrl is not defined, simply display the ErrorString.
//                    MessageBox.Show(wlex.ErrorString);
//                }
//            }

//            //Check that the username is valid.
//            if (Identity != null)
//            {
//                //Check to make sure the user has stored their username and password.
//                if (Identity.SavedCredentials == CredentialType.UserNameAndPassword)
//                {
//                    try
//                    {
//                        //Try silent authentication.
//                        if (Identity.Authenticate(AuthenticationType.Silent))
//                        {
//                            currentUserName = defaultUserName;
//                        }
//                        else
//                        {
//                            MessageBox.Show("Default user's stored sign-in name and password are invalid.");
//                        }
//                    }
//                    catch (WLLogOnException wlex)
//                    {
//                        //Check to see if FlowUrl is defined.
//                        if (wlex.FlowUrl != null)
//                        {
//                            //If FlowUrl is defined, direct user to the web page to correct the error.
//                            MessageBox.Show(wlex.ErrorString + "Please go to " + wlex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error");
//                        }
//                        else
//                        {
//                            //If FlowUrl is not defined, simply display the ErrorString.
//                            MessageBox.Show(wlex.ErrorString);
//                        }
//                    }
//                }
//                else
//                {
//                    MessageBox.Show("Default user hasn't stored sign-in name and password.");
//                }
//            }
//            else
//            {
//                MessageBox.Show("defaultUserName in config file has an invalid value.");
//                //config.AppSettings.Settings.Remove("defaultUserName");
//                //config.Save(ConfigurationSaveMode.Modified);
//                //ConfigurationManager.RefreshSection("appSettings");
//            }
//        }

//        internal void SignOut()
//        {
//            //If user is authenticated, they intended to Sign-Out. Try signing the user out.
//            try
//            {
//                WindowsLive.Instance.Identity.CloseIdentityHandle();
//                currentUserName = "";
//            }
//            catch (WLLogOnException wlex)
//            {
//                //Check to see if FlowUrl is defined.
//                if (wlex.FlowUrl != null)
//                {
//                    //If FlowUrl is defined, direct user to the web page to correct the error.
//                    MessageBox.Show(wlex.ErrorString + "Please go to " + wlex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error");
//                }
//                else
//                {
//                    //If FlowUrl is not defined, simply display the ErrorString.
//                    MessageBox.Show(wlex.ErrorString);
//                }
//            }
//        }

//        internal void SignIn()
//        {
//            try
//            {
//                //Try to authenticate the user by showing the Sign-In dialog window.
//                if (WindowsLive.Instance.Identity.Authenticate())
//                {
//                    currentUserName = WindowsLive.Instance.Identity.UserName;
//                }
//                else
//                {
//                    MessageBox.Show("Authentication failed");
//                }
//            }
//            catch (WLLogOnException wlex)
//            {
//                //Check to see if FlowUrl is defined.
//                if (wlex.FlowUrl != null)
//                {
//                    //If FlowUrl is defined, direct user to the web page to correct the error.
//                    MessageBox.Show(wlex.ErrorString + "Please go to " + wlex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error");
//                }
//                else
//                {
//                    //If FlowUrl is not defined, simply display the ErrorString.
//                    MessageBox.Show(wlex.ErrorString);
//                }
//            }
//        }
//    }
//}
